package com.study.examples.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study.examples.common.convert.BeanConvertHelper;
import com.study.examples.common.result.ResultCode;
import com.study.examples.dto.AddUserInfoRequest;
import com.study.examples.dto.AddUserInfoResponse;
import com.study.examples.dto.UserInfoListResponse;
import com.study.examples.mapper.UserInfoMapper;
import com.study.examples.model.UserInfo;
import com.study.examples.service.IUserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName: UserInfoServiceImpl
 * @Description:
 * @author: limingxing
 * @Date: 2020-01-15 15:40
 */
@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public AddUserInfoResponse create(AddUserInfoRequest request) {
        AddUserInfoResponse response = new AddUserInfoResponse();
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(request,userInfo);
        Integer row = userInfoMapper.insert(userInfo);
        if(row > 0 ){
            response.setCode(ResultCode.SUCCESS.getCode());
            response.setMsg(ResultCode.SUCCESS.getMessage());
        }else {
            response.setCode(ResultCode.DB_SAVE_EXCEPTION.getCode());
            response.setMsg(ResultCode.DB_SAVE_EXCEPTION.getMessage());
        }
        return response;
    }

    @Override
    public IPage<UserInfoListResponse> getList(int pageNum, int pageSize) {
        IPage<UserInfo> page = userInfoMapper.selectPage(new Page<>(pageNum,pageSize),new QueryWrapper());
        IPage<UserInfoListResponse> resp = BeanConvertHelper.convert(page, UserInfoListResponse::new);
        return resp;
    }



}
